package org.fhawk.tinyspring.core;

import org.apache.commons.lang.StringUtils;
import org.fhawk.tinyspring.Constant;
import org.fhawk.tinyspring.util.PropsUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;
import java.util.ResourceBundle;

/**
 * Created by tuo on 8/11/2016.
 */
public class ConfigHelper {
    private static Logger log = LoggerFactory.getLogger(ConfigHelper.class);

    private ConfigHelper() {
    }

    /**
     * 加载配置文件
     */
    private static final ResourceBundle SYS_PROPS = PropsUtil.loadProps(Constant.CONFIG_PROPS);

    /**
     * 从配置文件中获取一个值
     *
     * @param key 键
     * @return 值
     */
    public static String getString(String key) {
        String result = "";
        if (!StringUtils.isEmpty(key)) {
            try {
                result = SYS_PROPS.getString(key);
            } catch (Exception e) {
                log.debug("not config for key {} ", key);
            }
        }
        return result;
    }

    /**
     * 获取指定前缀的相关属性
     *
     * @param prefix 前缀
     * @return 指定前缀的所有配置项
     */
    public static Map<String, String> getMap(String prefix) {
        return PropsUtil.getMap(Constant.CONFIG_PROPS, prefix);
    }


}
